(require 'seq)

(defun oi-get-before-name (filename)
  "A function to get the name before ."
  (let ((i (- (length filename) 1)))
    (while (not (eq (aref filename i) ?.))
      (setf i (- i 1)))
    (seq-take filename i)))

(defun oi-get-after-name (filename)
  "A function to get the name after ."
  (let ((i (- (length filename) 1)))
    (while (not (eq (aref filename i) ?.))
      (setf i (- i 1)))
    (substring filename (+ i 1))))

(defun easy-compile ()
  "A command to compile one file easily."
  (interactive)
  (let ((filename (buffer-file-name)))
    (if (if filename (string= (oi-get-after-name filename) "cpp") nil)
	(shell-command (concat "g++ -g3 -o" (oi-get-before-name filename) " " filename))
      (shell-command "g++ -g3 -o src src.cpp"))))

(defun easy-compile-and-run ()
  "A command to compile and run easily."
  (interactive)
  (easy-compile)
  (let ((filename (buffer-file-name)))
    (if (if filename (string= (oi-get-after-name filename) "cpp") nil)
	(async-shell-command (concat "time " (oi-get-before-name filename)))
      (async-shell-command "time ./src"))))

(defun easy-gdb ()
  "A command to gdb easily."
  (interactive)
  (let ((filename (buffer-file-name)))
    (if (if filename (string= (oi-get-after-name filename) "cpp") nil)
	(gdb (concat "gdb -i=mi " (oi-get-before-name filename)))
      (gdb "gdb -i=mi src"))))

(defun lutts-init-oi-mode nil
  (add-hook 'evil-leader-mode-hook
            '(lambda ()
               (evil-leader/set-key
                 "ec" 'easy-compile
                 "er" 'easy-compile-and-run
                 "eg" 'easy-gdb))))

(lutts-load-package 'lutts-init-oi-mode)

(provide 'oi-mode)
